package com.javasm.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.commons.entity.AxiosResult;
import com.javasm.commons.entity.CrmProperties;
import com.javasm.commons.enums.E;
import com.javasm.commons.enums.OperatorAction;
import com.javasm.commons.enums.OperatorType;


import com.javasm.commons.exceptions.MvcException;
import com.javasm.dict.entity.SysDicData;
import com.javasm.dict.service.DicDataService;
import com.javasm.sys.annotations.MyLog;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("data")
public class DataController {
    @Resource
    private DicDataService dc;

    @GetMapping
    @MyLog(title = "字典模块",operationAction = OperatorAction.OTHER,operatorType = OperatorType.MANAGE)
    public AxiosResult getByPage(SysDicData sdd, Integer pageNum, Integer pageSize){
        if (pageNum==null)pageNum = 1;
        if (pageSize==null)pageSize = 10;
        PageHelper.startPage(pageNum,pageSize);
        List<SysDicData> list = dc.getList(sdd);
        if (list==null&&list.size()==0)throw new MvcException(E.NOT_EXISTS);
        PageInfo p = new PageInfo(list);
        return AxiosResult.suc(p);
    }

    @GetMapping("{type}")
    public AxiosResult getType(@PathVariable String type){
        SysDicData sd = new SysDicData();
        sd.setDicType(type);
        List<SysDicData> list = dc.getList(sd);
        return AxiosResult.suc(list);
    }

    @PostMapping
    @MyLog(title = "字典模块",operationAction = OperatorAction.INSERT,operatorType =
            OperatorType.MANAGE)
    public AxiosResult insertData(@RequestBody SysDicData sdd){
        boolean b = dc.insertAll(sdd);
        if (!b)throw new MvcException(E.OPERATION_FAILED);
        return AxiosResult.suc();
    }

    @PutMapping
    @MyLog(title = "字典模块",operationAction = OperatorAction.UPDATE,operatorType =
            OperatorType.MANAGE)
    public AxiosResult updateData(@RequestBody SysDicData sdd){
        boolean update = dc.update(sdd);
        if (!update)throw new MvcException(E.OPERATION_FAILED);
        return AxiosResult.suc();
    }

    @DeleteMapping
    @MyLog(title = "字典模块",operationAction = OperatorAction.DELETE,operatorType =
            OperatorType.MANAGE)
    public AxiosResult delDataByIds(String ids){
        String[] split = ids.split(",");
        boolean b = dc.delByIds(split);
        if (!b) throw new MvcException(E.OPERATION_FAILED);

        return AxiosResult.suc();
    }



}
